/*
 *  Copyright 2010 sonrisa.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package com.zh.jbet.core.dao;

import com.zh.jbet.core.entity.Game;
import java.util.List;
import javax.persistence.Query;

/**
 *
 * @author sonrisa
 */
public class GameDao<T extends Game> extends BaseJpaDao<T> {
    
    public List<T> findRootGames(Long rootId) {
        StringBuilder qstr = new StringBuilder("SELECT object(gm) FROM ");
        qstr.append(getEntityClass().getSimpleName());
        qstr.append(" gm WHERE gm.rootId = ?1 OR gm.id= ?1 ORDER BY gm.nestedStart");
        Query q = entityManager.createQuery(qstr.toString());
        q.setParameter(1, rootId);
        return (List<T>)q.getResultList();
    }
/*
    public List<T> findContextGames(Long contextId) {
        StringBuffer qstr = new StringBuffer("SELECT object(gm) FROM ");
        qstr.append(getEntityClass().getSimpleName() + " gm, ");
        qstr.append(GameContext.class.getSimpleName() + " gc ");
        qstr.append(" WHERE gc.id = ?1 AND gm.rootId = gc.rootId");
        qstr.append(" AND ((gm.parentId = gc.id) OR (gm.nestedId > gc.nestedId AND gm.nestedId < (select max(nestedId) FROM ");
        qstr.append(GameContext.class.getSimpleName() + " where parentId = gc.id)) ");
        qstr.append(" ORDER BY gm.nestedId");
        Query q = entityManager.createQuery(qstr.toString());
        q.setParameter(1, contextId);
        return (List<T>)q.getResultList();
    }
 * 
 */
}
